ggplot interactiveIn this lecture, we briefly introduce a few useful packages for building interactive visuals.
plotly: Add basic interactions to the plot made with ggplot2.
leaflet: For interactive map visualizations.
gganimate: For graphs with animation. The package gifski converts the animation into GIF.
Let’s first load the necessary plotting libraries.
plotlyFirst, let us create a static plot based on the iris data set.
plotlyplotly (https://plotly.com/) is a software that provides open-source APIs for interactive visuals.
In a wide variety of languages, including R, Python, Matlab, and JavaScript.
With the plotly package loaded, we can convert ggplot objects easily into interactive graphs.
Additionally, we can call plotly’s own functions to build interactive graphics.
The data below are produced from US economic time series available from the FRED.
psavert) and the number of unemployed in thousands (unemploy).For visuals with multiple time series, it is useful to include annotations or hover tools.
In the code below, we set the hovermode attribute to x unified.
x value as the cursor.leafletleaflet (https://leafletjs.com/) is an open-source JavaScript library for building interactive maps.
Maps built with leaflet have rich interactivity by default, including the ability to pan, zoom, hover, click on map elements, and markers.
We can create such a maps by calling the leaflet() function.
Map tiles: A set of small square images, each of which shows a single piece of map.
By default, leaflet uses tiles from OneStreetMap (https://www.openstreetmap.org).
We can replace the location pins as circle markers via addCircleMarkers().
gganimateLastly, let’s go through gganimate, which extends ggplot2 to include animation.
The transition_time() function defines how the data relate to itself across time.
It also creates label variables we can pass to the title/subtitle via {frame_time}.
To save the animation as a GIF, use the following code.
p2 <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, size = pop, color = country)) +
geom_point(size = 2, show.legend = FALSE) +
labs(title = "Health vs Wealth, all years", x = "GDP per capita", y = "Life expectancy") +
scale_x_continuous(trans = "log10", labels = scales::label_dollar(accuracy = 1)) +
theme_bw()
p3 <- p2 +
labs(title = "Wealth vs Health",
subtitle = "Year: {frame_time}", x = "GDP per capita", y = "Life expectancy") +
transition_time(year)
animate(p3, renderer = gifski_renderer(), res = 150)Let’s explore the evolution of life expectancy across continents and years.
p4 <- gapminder %>%
group_by(continent, year) %>%
summarize(lifeExp = mean(lifeExp, na.rm = TRUE)) %>%
ggplot(aes(x = year, y = lifeExp, color = continent)) +
geom_line(lwd = 2) +
labs(title = "Life expectancy across time", x = "Year", y = "Life expectancy", color = "") +
theme(legend.position = "top", legend.margin=margin())
p4Here, the transition_reveal() function is used to reveal new time frame progressively.
The field of visualization is moving fast - new work is constantly emerging; new products are constantly being released.
Early and fundamental books:
Exploratory Data Analysis by John Tukey (1977).
The Visual Display of Quantitative Information by Edward Tufte (2001).
The Fundamental Art: An Introduction to Information Graphics and Visualization by Alberto Cairo (2013).
Blogs dedicated to data visualization:
Eager eyes by Robert Kosara.
Flowing Data by Nathan Yau.
Storytelling with Data by Cole Nussbaumer.
PolicyViz by Jonathan Schwabish.